//
//  LSLeagueHomePageCell.m
//  LookScore
//
//  Created by wr on 2018/1/31.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//
#define RATIO_WIDTH    LSAutoVlaue(77, LSScreenWidth)
//77
#import "LSLeagueHomePageCell.h"
#import "LSCustomHeaderView.h"
#import "LSArrowsView.h"
@interface LSLeagueHomePageCell()

@property(strong,nonatomic)UIImageView *logoImg;

@property(strong,nonatomic)UILabel *leagueNameLab;

@property(strong,nonatomic)UILabel *leagueLab;

@property(strong,nonatomic)UILabel *sessionLab;

@property(strong,nonatomic)UILabel *fisihSessionLab;//

@end

@implementation LSLeagueHomePageCell

+(LSLeagueHomePageCell *)LeagueHomePageCellByTableView:(UITableView *)tableView{
    LSLeagueHomePageCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    if (!cell) {
        cell=[[LSLeagueHomePageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(self)];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        [self addContentView];
    }
    
    return self;
}

-(void)addContentView{
   
    UIImageView *logoImg=[[UIImageView alloc]init];
    [self.contentView addSubview:logoImg];
    self.logoImg=logoImg; logoImg.sd_layout.leftEqualToView(self.contentView).offset(LSSpaceLeftWidth1).topEqualToView(self.contentView).offset(16).widthIs(LSAutoVlaue(50, LSScreenWidth)).bottomEqualToView(self.contentView).offset(-25);
    
    UILabel *leagueNameLab=[[UILabel alloc]init];
    leagueNameLab.font=LSFontSize13;
    leagueNameLab.textColor=LSSubColor5D5D5D;
    [self.contentView addSubview:leagueNameLab];
    self.leagueNameLab=leagueNameLab;
    leagueNameLab.sd_layout.leftSpaceToView(logoImg, 10).topEqualToView(logoImg).heightIs(20).widthIs(LSScreenWidth*.5);
    
    UILabel *leagueLab=[[UILabel alloc]init];
    leagueLab.font=LSFontSize13;
    leagueLab.textColor=LSSubColorB5B3B3;
    [self.contentView addSubview:leagueLab];
    self.leagueLab=leagueLab;
    leagueLab.sd_layout.leftEqualToView(leagueNameLab).topSpaceToView(leagueNameLab, 0).bottomEqualToView(logoImg).widthIs(LSScreenWidth*.5);
    
    UILabel *sessionLab=[[UILabel alloc]init];
    sessionLab.font=LSFontSize12;
    sessionLab.textColor=LSNavMainColor;
    sessionLab.textAlignment=NSTextAlignmentRight;
    [self.contentView addSubview:sessionLab];
    self.sessionLab=sessionLab;
    sessionLab.sd_layout.leftSpaceToView(leagueNameLab, 10).topEqualToView(leagueNameLab).heightRatioToView(leagueNameLab, 1).rightEqualToView(self.contentView).offset(-LSSpaceLeftWidth1);
    
    UILabel *fisihSessionLab=[[UILabel alloc]init];
    fisihSessionLab.font=LSFontSize9;
    fisihSessionLab.textColor=LSSubColorB5B3B3;
    fisihSessionLab.textAlignment=NSTextAlignmentRight;
    [self.contentView addSubview:fisihSessionLab];
    self.fisihSessionLab=fisihSessionLab;
    fisihSessionLab.isAttributedContent=YES;
    fisihSessionLab.sd_layout.leftEqualToView(sessionLab).topSpaceToView(sessionLab, 0).bottomEqualToView(leagueLab).rightEqualToView(self.contentView).offset(-LSSpaceLeftWidth1);
    
    UIView *lineView=[[UIView alloc]init];
    lineView.backgroundColor=LSBackGroundColor;
    [self.contentView addSubview:lineView];
    lineView.sd_layout.leftEqualToView(self.contentView).bottomEqualToView(self.contentView).rightEqualToView(self.contentView).heightIs(9);
   
}

- (void)setModel:(LSLeagueHomePageModel *)model{
    _model = model;
    
    self.logoImg.image=[UIImage imageNamed:@"MineDefaultHead"];
//    self.logoImg.image=[UIImage imageNamed:self.model.PictureName];
    self.leagueNameLab.text=self.model.LeagueName;
    self.leagueLab.text=self.model.LeagueEnglishName;
    self.sessionLab.text=[NSString stringWithFormat:@"当前第%@轮",self.model.Rounding];
    
    NSString *changeTitle = [NSString stringWithFormat:@"%@/%@",self.model.Rounding,self.model.TotalRound];
    
    self.fisihSessionLab.attributedText=[CommonClass setBasicTitle:@"已完" homeTitle:@"轮" changeTitle:changeTitle color:LSSubColorf25e59 size:9 isBold:NO];
}








- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

#pragma mark 🔥 LSLeagueHomePageRatioCell

@interface LSLeagueHomePageRatioCell()

@property(strong,nonatomic)UIView *chooseView;

@property(strong,nonatomic)LSArrowsView *arrowsView;

@property(strong,nonatomic)NSMutableArray *controlArray;

@end

@implementation LSLeagueHomePageRatioCell{
    LSCustomHeaderView *_Nullable headerView;
}

-(NSMutableArray *)controlArray{
    
    if (!_controlArray) {
        _controlArray=[NSMutableArray array];
        
    }
    return _controlArray;
}
+(LSLeagueHomePageRatioCell *)LeagueHomePageRatioCellByTableView:(UITableView *)tableView{
    LSLeagueHomePageRatioCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    if (!cell) {
        cell=[[LSLeagueHomePageRatioCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(self)];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        [self addContentView];
    }
    
    return self;
}

-(void)addContentView{
    
    NSArray *titleArray=@[@"胜平负",@"亚盘",@"大小球"];
    
   headerView=[[LSCustomHeaderView alloc]initWithReuseIdentifier:@"LSLeagueHomePageRatioCell" style:KTableViewHeaderTitleStyle];
    headerView.contentView.backgroundColor=[UIColor whiteColor];
    headerView.timeLab.text=@"已赛100场";
    [self.contentView addSubview:headerView];
    headerView.sd_layout.leftEqualToView(self.contentView).topEqualToView(self.contentView).rightEqualToView(self.contentView).heightIs(40);
    
    UIView *chooseView=[[UIView alloc]init];
    [self.contentView addSubview:chooseView];
    self.chooseView=chooseView;
    chooseView.sd_layout.centerXEqualToView(self.contentView).topSpaceToView(headerView, 16).widthIs(RATIO_WIDTH*titleArray.count).heightIs(40);
    
 
    for (int i=0; i<titleArray.count; i++) {
      
        UIButton  *chooseButton=[UIButton buttonWithType :UIButtonTypeCustom];
        [chooseButton setTitleColor:LSSubColorB4B4B4 forState:UIControlStateNormal];
        [chooseButton setTitle:titleArray[i] forState:UIControlStateNormal];
        chooseButton.titleLabel.font=LSFontSize12;
        chooseButton.backgroundColor=LSSubGrayColorf7;
        chooseButton.tag=10000+i;
        [chooseButton addTarget:self action:@selector(chooseRationAction:) forControlEvents:UIControlEventTouchUpInside];
        [chooseView  addSubview:chooseButton];
         chooseButton.sd_layout.leftEqualToView(self.contentView).offset(RATIO_WIDTH*i).topEqualToView(chooseView).widthIs(RATIO_WIDTH).bottomEqualToView(chooseView).offset(-8);
        if (i!=titleArray.count-1) {
     
        UIView *lineView=[[UIView alloc]init];
        lineView.backgroundColor=LSSubColorB4B4B4;
        [chooseButton addSubview:lineView];
        lineView.sd_layout.leftEqualToView(chooseButton).offset(RATIO_WIDTH-1).topEqualToView(chooseButton).offset(8).widthIs(.5).bottomEqualToView(chooseButton).offset(-8);
            
        }
        if (i==0) {
            chooseButton.selected=YES;
           [chooseButton setTitleColor:LSNavMainColor forState:UIControlStateSelected];;
            
            LSArrowsView *arrowsView=[[LSArrowsView alloc]init];
            arrowsView.backgroundColor=[UIColor whiteColor];
            [chooseView addSubview:arrowsView];
            self.arrowsView=arrowsView;
            arrowsView.sd_layout.leftEqualToView(self.contentView).offset(RATIO_WIDTH*i+(RATIO_WIDTH-15)/2).topSpaceToView(chooseButton, 0).widthIs(15).heightIs(8);
        }

        
//ratio
        UILabel *ratioLabel=[[UILabel alloc]init];
        ratioLabel.font=LSFontSize13;
        ratioLabel.textColor=LSTitleGrayColor3;
        ratioLabel.textAlignment=NSTextAlignmentCenter;
        [self.contentView addSubview:ratioLabel];
        
        [self.controlArray addObject:ratioLabel];
        
        UILabel *barChartLabel=[[UILabel alloc]init];
        barChartLabel.font=LSFontSize13;
        barChartLabel.textColor=LSTitleGrayColor3;
        [self.contentView addSubview:barChartLabel];
        [self.controlArray addObject:barChartLabel];
        
        UILabel *barChartBottomLabel=[[UILabel alloc]init];
        barChartBottomLabel.font=LSFontSize13;
        barChartBottomLabel.textColor=LSTitleGrayColor3;
        [self.contentView addSubview:barChartBottomLabel];
        [self.controlArray addObject:barChartBottomLabel];
        
        
        UILabel *gameTypeLabel=[[UILabel alloc]init];
        gameTypeLabel.font=LSFontSize13;
        gameTypeLabel.textColor=[UIColor blackColor];
        gameTypeLabel.textAlignment=NSTextAlignmentCenter;
        [self.contentView addSubview:gameTypeLabel];
       
        [self.controlArray addObject:gameTypeLabel];
//        ratioLabel.sd_layout.leftEqualToView(chooseView).offset(chooseView.left_sd+RATIO_WIDTH*i).topSpaceToView(chooseView, 22).widthIs(RATIO_WIDTH-20).heightIs(15);
        ratioLabel.sd_layout.leftEqualToView(self.contentView).offset((LSScreenWidth-RATIO_WIDTH*3)/2+RATIO_WIDTH*i).topSpaceToView(chooseView, 22).widthIs(RATIO_WIDTH-20).heightIs(15);
        
        gameTypeLabel.sd_layout.leftEqualToView(self.contentView).offset((LSScreenWidth-RATIO_WIDTH*3)/2+RATIO_WIDTH*i).widthIs(RATIO_WIDTH-20).heightIs(15).bottomEqualToView(self.contentView).offset(-20);
        barChartBottomLabel.sd_layout.leftEqualToView(self.contentView).offset((LSScreenWidth-RATIO_WIDTH*3)/2+RATIO_WIDTH*i).widthIs(RATIO_WIDTH-20).heightIs(2).bottomSpaceToView(gameTypeLabel,13);
        
         barChartLabel.sd_layout.leftEqualToView(self.contentView).offset((LSScreenWidth-RATIO_WIDTH*3)/2+RATIO_WIDTH*i).widthIs(RATIO_WIDTH-20).heightIs(15).bottomSpaceToView(barChartBottomLabel, 0);
        
        
        
    }
    
    UIView *lineView=[[UIView alloc]init];
    lineView.backgroundColor=LSBackGroundColor;
    [self.contentView addSubview:lineView];
    lineView.sd_layout.leftEqualToView(self.contentView).bottomEqualToView(self.contentView).rightEqualToView(self.contentView).heightIs(9);
//    [self addfalseData];
    
}

//- (void)setSelectTag:(NSInteger)selectTag{
//    _selectTag = selectTag;
//     [self addfalseData:_model withType:selectTag];
//}
- (void)setModel:(LSLeagueHomePageModel *)model{
    _model = model;
    headerView.timeLab.text  = [NSString stringWithFormat:@"已赛%@场",self.model.MatchingCount];
    [self addfalseData:model withType:_selectTag];
}
-(void)addfalseData:(LSLeagueHomePageModel * )model withType:(NSInteger)type{

    NSString * leftStr;
    NSString * midStr;
    NSString * rightStr;
    if (type == 0) {
        leftStr = model.SPF_SProba;
        midStr = model.SPF_PProba;
        rightStr = model.SPF_FProba;
    }else if (type == 1){
        leftStr = model.YP_YProba;
        midStr = model.YP_ZProba;
        rightStr = model.YP_SProba;
        
    }else{
        leftStr = model.Dx_DProba;
        midStr = model.Dx_ZProba;
        rightStr = model.Dx_XProba;
    }
    
    [self.controlArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *label=(UILabel *)obj;
        if (idx==0) {
            label.text= [NSString stringWithFormat:@"%@%%",leftStr];
            label.textColor=LSTitleGrayColor3;
           
        }else if (idx==1){
            label.backgroundColor=Color(236, 169, 164);
//            label.backgroundColor=LSSubColorf25e59;
//            label.sd_layout.heightIs(45);
            CGFloat h = 60*leftStr.floatValue/100;
            label.sd_layout.heightIs(h);
        }else if (idx==2){
            label.backgroundColor=Color(224, 100, 88);
//            label.backgroundColor=LSSubColorDF1C1C;
        }else if (idx==3){
            label.text=@"主胜";
            label.textColor=[UIColor blackColor];
        }else if (idx==4){
            label.text= [NSString stringWithFormat:@"%@%%",midStr];
            label.textColor=LSTitleGrayColor3;
           
        }else if (idx==5){
              label.backgroundColor=Color(174, 211, 234);
//              label.backgroundColor=LSSubColor75b8fe;
//              label.sd_layout.heightIs(25);
            CGFloat h = 60*midStr.floatValue/100;
            label.sd_layout.heightIs(h);
        }else if (idx==6){
              label.backgroundColor=Color(100, 172, 217);
//             label.backgroundColor=LSSubColor5cd2df;
          
        }else if (idx==7){
            label.text=@"平局";
            label.textColor=[UIColor blackColor];
        }else if (idx==8){
            label.text=[NSString stringWithFormat:@"%@%%",rightStr];
            label.textColor=LSTitleGrayColor3;
            
        }else if (idx==9){
              label.backgroundColor=Color(154, 195, 172);
//            label.backgroundColor=LSSubColor9dd3cc;
//              label.sd_layout.heightIs(31);
            CGFloat h = 60*rightStr.floatValue/100;
            label.sd_layout.heightIs(h);
        }else if (idx==10){
              label.backgroundColor=Color(70, 144, 101);
//            label.backgroundColor=LSNavMainColor;
        }else if (idx==11){
            label.text=@"客胜";
            label.textColor=[UIColor blackColor];
        }
        [self.contentView layoutIfNeeded];

    }];
}



-(void)chooseRationAction:(UIButton *)sender{
    if (!sender.selected) {
        sender.selected=!sender.selected;
        [self addfalseData:_model withType:sender.tag -10000];
        for (UIButton *chooseButton in self.chooseView.subviews) {
            
            if ([chooseButton isKindOfClass:[UIButton class]]) {
                if (chooseButton.tag==sender.tag) {
                    chooseButton.selected= sender.selected;
                    [chooseButton setTitleColor:LSNavMainColor forState:UIControlStateSelected];
                    self.arrowsView.sd_resetNewLayout.leftEqualToView(self.chooseView).offset(RATIO_WIDTH*(sender.tag-10000)+(RATIO_WIDTH-15)/2).topEqualToView(self.chooseView).offset(32).widthIs(15).heightIs(8);
              
                    [self.chooseView layoutSubviews];
                    [self.chooseView layoutIfNeeded];
                
                    if (self.butonActionBlock) {
                        self.butonActionBlock(chooseButton.tag-10000);
                    }
                }else{
                    chooseButton.selected=!sender.selected;
                    [chooseButton setTitleColor:LSSubColorB4B4B4 forState:UIControlStateNormal];
                }
            }
        }
    }
}

@end

#pragma mark 🔥 LSLeagueHomePageDataCell

@interface LSLeagueHomePageDataCell()
@property (nonatomic, strong) LSCustomHeaderView *_Nullable headerView;

@end

@implementation LSLeagueHomePageDataCell

+(LSLeagueHomePageDataCell *)LeagueHomePageDataCellByTableView:(UITableView *)tableView{
    LSLeagueHomePageDataCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    if (!cell) {
        cell=[[LSLeagueHomePageDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(self)];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        [self addContentView];
    }
    
    return self;
}

-(void)addContentView{
    
    self.headerView=[[LSCustomHeaderView alloc]initWithReuseIdentifier:@"LSLeagueHomePageDataCell" style:KTableViewHeaderTitleStyle];
    self.headerView.contentView.backgroundColor=[UIColor whiteColor];
    self.headerView.timeLab.text=@"已赛0场";
    [self.contentView addSubview:self.headerView];
     self.headerView.sd_layout.leftEqualToView(self.contentView).topEqualToView(self.contentView).rightEqualToView(self.contentView).heightIs(40);
    
    NSArray *totalArr = @[@"总进球",@"主场进球",@"客场进球"];
    NSArray *totalGoalsArr = @[@"0",@"0",@"0"];
    
    NSArray *averageArr = @[@"平均进球",@"主场场均进球",@"客场场均进球"];
    NSArray *averageGoalsArr = @[@"0",@"0",@"0"];
    
    UIView *totalLine = [[UIView alloc] init];
    totalLine.backgroundColor = Color(224, 100, 88);
    [self.contentView addSubview:totalLine];
    totalLine.sd_layout.topSpaceToView(self.headerView, 30).leftEqualToView(self.contentView).offset(50).heightIs(60).widthIs(2);
    
    UIView *averageLine = [[UIView alloc] init];
    averageLine.backgroundColor = Color(70, 144, 101);
    [self.contentView addSubview:averageLine];
    averageLine.sd_layout.topSpaceToView(totalLine, 10).leftEqualToView(totalLine).heightIs(60).widthIs(2);
    
    for (int i = 0; i < 3; i ++) {
        UILabel *totalGoals = [[UILabel alloc] init];
        totalGoals.font = LSFontSize15;
        totalGoals.text = totalGoalsArr[i];
        totalGoals.textAlignment = NSTextAlignmentCenter;
        totalGoals.tag = 100 + i;
        [self.contentView addSubview:totalGoals];
        totalGoals.sd_layout.widthIs((self.contentView.width - 50)/3).leftSpaceToView(totalLine, i*totalGoals.width).topEqualToView(totalLine).heightIs(totalLine.height/2);
        
        UILabel *totalTitle = [[UILabel alloc] init];
        totalTitle.text = totalArr[i];
        totalTitle.font =LSFontSize12;
        totalTitle.backgroundColor = LSSubGrayColorf7;
        totalTitle.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:totalTitle];
        totalTitle.sd_layout.leftSpaceToView(totalLine, i*totalGoals.width).topSpaceToView(totalGoals, 0).widthIs(totalGoals.width).heightIs(totalGoals.height);
        
        UILabel *averageGoals = [[UILabel alloc] init];
        averageGoals.font = LSFontSize15;
        averageGoals.text = averageGoalsArr[i];
        averageGoals.textAlignment = NSTextAlignmentCenter;
        averageGoals.tag = 200 + i;
        [self.contentView addSubview:averageGoals];
        averageGoals.sd_layout.widthIs((self.contentView.width - 50)/3).leftSpaceToView(averageLine, i*averageGoals.width).topEqualToView(averageLine).heightIs(averageLine.height/2);
//
        UILabel *averageTitle = [[UILabel alloc] init];
        averageTitle.text = averageArr[i];
        averageTitle.font =LSFontSize12;
        averageTitle.backgroundColor = LSSubGrayColorf7;
        averageTitle.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:averageTitle];
        averageTitle.sd_layout.leftSpaceToView(averageLine, i*averageGoals.width).topSpaceToView(averageGoals, 0).widthIs(averageGoals.width).heightIs(averageGoals.height);
        
        if (i==0) {
            totalGoals.textColor = totalLine.backgroundColor;
            totalTitle.textColor = totalLine.backgroundColor;
            
            averageGoals.textColor = averageLine.backgroundColor;
            averageTitle.textColor = averageLine.backgroundColor;
        }else{
            totalGoals.textColor = LSTitleGrayColor2;
            totalTitle.textColor = LSTitleGrayColor2;
            
            averageGoals.textColor = LSTitleGrayColor2;
            averageTitle.textColor = LSTitleGrayColor2;
        }
    }
    
}
- (void)setModel:(LSLeagueHomePageModel *)model{
    _model = model;
    
    self.headerView.timeLab.text = [NSString stringWithFormat:@"已赛%@场",self.model.MatchingCount];
    
    NSMutableArray *totalGoalsArr = [NSMutableArray arrayWithObjects:self.model.AllTotalNumber,self.model.HomeTotalNumber,self.model.GestTotalNumber, nil];
    
    NSMutableArray *averageGoalsArr = [NSMutableArray arrayWithObjects:self.model.AllAvgNumber,self.model.HomeAvgNumber,self.model.GestAvgNumber, nil];
    
    for (int i = 0; i < 3; i ++) {
        UILabel *totalGoals = [self viewWithTag:100 + i];
        totalGoals.text = totalGoalsArr[i];

        UILabel *averageGoals = [self viewWithTag:200 + i];
        averageGoals.text = averageGoalsArr[i];
    }
}

@end


